#include <setjmp.h>
#include "cpu/reg.h"
#include "memory/memory.h"
#include "common.h"
#include "mmu.h"

extern jmp_buf jbuf;
lnaddr_t seg_translate(swaddr_t addr, size_t len, uint8_t sreg);

void raise_intr(uint8_t NO){
  /*Save the status*/
 cpu.esp-=4;
 current_sreg = R_SS;
  swaddr_write(cpu.esp,4,cpu.eflags.val);
  cpu.esp-=2;
  swaddr_write(cpu.esp,2,cpu.cs.selector);
  cpu.esp-=4;
  swaddr_write(cpu.esp,4,cpu.eip);

  uint32_t low= lnaddr_read(cpu.idtr.baseAddr+sizeof(struct GateDescriptor)*NO,4);
  uint32_t high= lnaddr_read(cpu.idtr.baseAddr+sizeof (struct GateDescriptor)*NO +4,4);
  cpu.cs.selector=(low >> 16)&0xFFFF;
  sreg_load(R_CS);
  uint32_t offset=(high & 0xFFFF0000)|(low & 0xFFFF);
 // cpu.eip = seg_translate(offset,4,R_CS);
  //printf("%d",offset);
  //printf("%d",offset);
  cpu.eip = offset;

  longjmp(jbuf,1);
}
